﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace MingYuanYun.Exception
{
    /// <summary>
    /// 异常断言工具类
    /// </summary>
    public class ExceptionAssert
    {
        /// <summary>
        /// 断言成功时，抛出异常信息 $"{message} 不能为空"
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="message"></param>
        public static void IsNull<T>(T t, string message = "")
        {
            if (typeof(T) == typeof(string))
            {
                if (string.IsNullOrEmpty(t as string))
                {
                    throw new BusinessException($"{message} 不能为空");
                }
            }
            else if (t == null)
            {
                throw new BusinessException($"{message} 不能为空");
            }
        }

        /// <summary>
        /// 断言成功时，抛出异常信息 $"{message}"
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="message"></param>
        public static void IsNullOrEmpty(string t, string message = "")
        {
            if (string.IsNullOrEmpty(t))
            {
                throw new BusinessException($"{message}");
            }
        }

        /// <summary>
        /// 断言成功时，抛出异常信息 $"{message}"
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="message"></param>
        public static void IsTrue(bool condition,string message = "")
        {
            if (condition)
            {
                throw new BusinessException(message);
            }
        }
    }
}
